package apptoko.com;

import android.content.res.Resources;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import apptoko.com.fragment.DownloadedFragment;
import apptoko.com.util.Constants;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import apptoko.com.R;
import com.astuetz.viewpager.extensions.PagerSlidingTabStrip;
import com.google.analytics.tracking.android.EasyTracker;

public class DownloadedFileManagerActivity extends SherlockFragmentActivity {

	private Resources res;
	private FileManagerFragmentAdapter adapter;
	private ViewPager viewPager;
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		setThemeLowEndDevice();
		super.onCreate(arg0);
		setContentView(R.layout.list_pager);
		EasyTracker.getInstance().setContext(this);
		res = getResources();
		getSupportActionBar().setTitle(R.string.downloaded_file_manager);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		adapter = new FileManagerFragmentAdapter(getSupportFragmentManager());
		viewPager = (ViewPager) findViewById(R.id.pager);
		
		DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
		int pageMargin = dm.widthPixels / 48;
		viewPager.setOffscreenPageLimit(6);
		viewPager.setPageMargin(pageMargin);
		viewPager.setPageMarginDrawable(R.drawable.bg_pagemargin_xml);
		viewPager.setAdapter(adapter);
		PagerSlidingTabStrip indicator = (PagerSlidingTabStrip) findViewById(R.id.indicator);
        indicator.setViewPager(viewPager);
        indicator.setIndicatorColorResource(R.color.comic_indicator);
        int pixelSize = (int) (14 * dm.scaledDensity); 
        indicator.setTextSize(pixelSize);
	}
	
	private void setThemeLowEndDevice(){
		if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.action_bar_comic);
            bg.setTileModeX(TileMode.REPEAT);
            getSupportActionBar().setBackgroundDrawable(bg);
        }
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private class FileManagerFragmentAdapter extends FragmentStatePagerAdapter {

		protected String[] TITLE = new String[] {res.getString(R.string.apps_downloaded)};

		public FileManagerFragmentAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			DownloadedFragment frag = new DownloadedFragment();
			Bundle bundle = new Bundle();
			bundle.putString(Constants.DOWNLOADED_TYPE, TITLE[position]);
			frag.setArguments(bundle);
			return frag;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return TITLE.length;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return TITLE[position];
		}
		
		@Override
		public void setPrimaryItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			if(position > 0){
				DownloadedFragment frag = (DownloadedFragment) object;
				frag.init();
			}
		}
	}
}
